<?php

/**
 * This is the model class for table "category".
 *
 * The followings are the available columns in table 'category':
 * @property integer $id
 * @property string $name
 * @property integer $parent
 */
class Category extends CommonActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * 
	 * @param string $className
	 *        	active record class name.
	 * @return Cate the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'category';
	}
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array (
				array (
						'name',
						'required' 
				),
				array (
						'parent',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'name',
						'length',
						'max' => 45 
				),
				// The following rule is used by search().
				// Please remove those attributes that should not be searched.
				array (
						'id, name, parent',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations() {
		return array (
				'category_parent'=>array(self::BELONGS_TO, 'Category', 'parent')
		);
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
				'id' => Yii::t ( 'category', 'category_id' ),
				'name' => Yii::t ( 'category', 'category_name' ),
				'parent' => Yii::t ( 'category', 'category_parent' ) 
		);
	}
	
	

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * 
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		$criteria->compare ( 'name', $this->name, true );
		$criteria->compare ( 'parent', $this->parent );
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	}
	
	/**
	 */
	public function getAllCategories($parentId = 0, &$cateList = array()) {
		$categories = $this->findAllByAttributes( array (
				'parent' => $parentId
		));
		if(empty($categories)){
			return array();
		}
		foreach($categories as $cate){
			$cateList[] = array(
					'id'=>$cate->id,
					'name'=>$cate->name,
					'parent'=>$cate->parent,
					'flag'=>1
			);
			$this->getAllCategories($cate->id,$cateList);
		}
		return $cateList;
	}
	
	public function getCategoriesList($parentId = 0) {
		$categories = $this->findAllByAttributes( array (
				'parent' => $parentId
		));
		if(empty($categories)){
			return false;
		}
		foreach($categories as $cate){
			$cateList[] = array(
					'id'=>$cate->id,
					'name'=>$cate->name,
					'parent'=>$cate->parent,
					'child'=>$this->getCategoriesList($cate->id)
			);
		}
		return $cateList;
	}
	
	
	public function getCategories($parentId = 0, &$cateList = array(),$level = 0) {
		$categories = $this->findAllByAttributes( array (
				'parent' => $parentId
		));
		if(empty($categories)){
			return array();
		}
		foreach($categories as $cate){
			$cateList[$cate->id] = str_repeat('-', $level*3).$cate->name;
			$this->getCategories($cate->id,$cateList,$level+1);
		}
		return $cateList;
	}
}